package com.expense.common.amiExt;

import cn.hutool.core.convert.Convert;
import com.expense.common.model.base.BaseModel;
import org.beetl.sql.core.SQLManager;
import org.beetl.sql.core.mapper.MapperInvoke;

import java.lang.reflect.Field;
import java.lang.reflect.Method;

public class SaveAmiExt implements MapperInvoke {
    public SaveAmiExt() {
    }

    @Override
    public Object call(SQLManager sqlManager, Class aClass, String s, Method method, Object[] objects) {
        if(objects == null || objects[0] == null) return null;
        BaseModel baseModel = (BaseModel) objects[0];

//        Field[] fields = aClass.getFields();
        Long id = Convert.toLong(baseModel.getId(),0l);
//        try {
//            for(int i = 0; i < fields.length; i++){
//                if("id".equals(fields[i].getName())){
//                    Field subField = fields[i];
//                    subField.setAccessible(true);
//                    Object o = subField.get(objects[0]);
//                    id = Convert.toLong(o,0l);
//                    break;
//                }
//            }
//        }catch (Exception e) {
//            throw new RuntimeException(e);
//        }
        if(id <= 0) {
            return sqlManager.insert(aClass,objects[0],true);
        }else {
            return sqlManager.updateTemplateById(aClass,objects[0]);
        }
    }
}
